﻿using CommunityToolkit.Mvvm.ComponentModel;
using DataCollection.Core.Models;
using DataCollection.Infrastructure;
using Mapster;
using Masuit.Tools;
using NetTaste;

namespace DataCollection.WinForm.ViewModel
{
    internal partial class ProjectViewModel : ObservableObject
    {
        #region 属性
        private readonly ProjectModel model = new ProjectModel();

        [ObservableProperty]
        private long number;

        [ObservableProperty]
        private string name = string.Empty;

        [ObservableProperty]
        private string stakeMark = string.Empty;

        [ObservableProperty]
        private string pipeSpecification = string.Empty;

        [ObservableProperty]
        private string pipeLength = string.Empty;

        [ObservableProperty]
        private string medium = string.Empty;

        [ObservableProperty]
        private string designPressure = string.Empty;

        [ObservableProperty]
        private string pipeNature = string.Empty;

        [ObservableProperty]
        private string testType = string.Empty;

        [ObservableProperty]
        private string company = string.Empty;

        [ObservableProperty]
        private string manometerPrecision = string.Empty;

        [ObservableProperty]
        private string manometerRange = string.Empty;

        [ObservableProperty]
        private string templateID = string.Empty;
        #endregion

        public ProjectViewModel(long number)
        {
            Dictionary<string, object> where = new Dictionary<string, object>();
            where["number"] = number.ToString();
            List<ProjectModel> list = ProjectModel.List(where);
            if (list.Count > 0)
            {
                this.model = list.First<ProjectModel>();
                this.model.Adapt(this);
            }
            else
            {
                this.number = number;
            }
        }

        public void Apply()
        {
            this.Adapt(this.model);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public Response Add()
        {
            Response response = Response.DefaultResponse();

            List<ProjectModel> list = List(Name);
            if (list.Count > 0)
            {
                response.Msg = string.Format("【{0}】已存在", Name);
                return response;
            }

            int res = this.model.Add();
            if (res == 1)
            {
                response.Code = Response.SuccessCode;
                response.Msg = "添加成功";
            }
            else
            {
                response.Msg = "添加失败";
            }
            return response;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public Response Delete()
        {
            Response response = Response.DefaultResponse();
            int res = this.model.Delete();
            if (res == 1)
            {
                response.Code = Response.SuccessCode;
                response.Msg = "删除成功";
            }
            else
            {
                response.Msg = "删除失败";
            }
            return response;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        public Response Update()
        {
            Response response = Response.DefaultResponse();
            int res = this.model.Update();
            if (res == 1)
            {
                response.Code = Response.SuccessCode;
                response.Msg = "编辑成功";
            }
            else
            {
                response.Msg = "编辑失败";
            }
            return response;
        }

        /// <summary>
        /// 根据名称查询
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<ProjectModel> List(string name)
        {
            Dictionary<string, object> where = new Dictionary<string, object>();
            where["name"] = name;
            return SqlSugarHelper.Inquireable<ProjectModel>(where);
        }
    }
}
